<!DOCTYPE html>
<html>
<head>
    <title>Kimi API 调试工具</title>
    <meta charset="UTF-8">
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; margin: 0; padding: 20px; background-color: #f8f9fa; display: flex; justify-content: center; }
        .container { max-width: 1200px; width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
        .panel { background-color: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        h1, h2 { color: #333; border-bottom: 1px solid #eee; padding-bottom: 10px; }
        label { display: block; margin-bottom: 5px; font-weight: 500; color: #555; }
        input[type="text"], input[type="url"], textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-family: monospace;
        }
        textarea { height: 250px; resize: vertical; }
        button {
            padding: 12px 25px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.2s;
        }
        button:hover { background-color: #0056b3; }
        #response-container { white-space: pre-wrap; word-wrap: break-word; background-color: #282c34; color: #abb2bf; padding: 15px; border-radius: 4px; font-family: 'Courier New', Courier, monospace; }
        .status { padding: 10px; border-radius: 4px; margin-top: 15px; }
        .status.success { background-color: #e9f7ef; color: #1e6641; }
        .status.error { background-color: #fbe9e9; color: #a82a2a; }
        @media (max-width: 900px) {
            .container { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="panel">
            <h1>Kimi API 调试工具</h1>
            <div>
                <label for="apiKey">API 密钥 (API Key):</label>
                <input type="text" id="apiKey" placeholder="在此输入您的 API 密钥 (例如 sk-...)">
            </div>
            <div>
                <label for="endpointUrl">API 端点 (Endpoint URL):</label>
                <input type="url" id="endpointUrl" value="https://api.moonshot.cn/v1/chat/completions">
            </div>
            <div>
                <label for="requestBody">请求体 (Request Body):</label>
                <textarea id="requestBody"></textarea>
            </div>
            <button onclick="sendRequest()">发送请求</button>
        </div>
        <div class="panel">
            <h2>响应结果 (Response)</h2>
            <div id="status"></div>
            <pre id="response-container">等待请求...</pre>
        </div>
    </div>

    <script>
        const defaultRequestBody = {
            "model": "moonshot-v1-8k",
            "messages": [
                {
                    "role": "system",
                    "content": "你是 Kimi，由 Moonshot AI 提供的人工智能助手，你更擅长中文和英文的对话。你会为用户提供安全，有帮助，准确的回答。同时，你会拒绝一些涉及恐怖主义，种族歧视，黄色暴力等问题的回答。Moonshot AI 为专有名词，不可翻译成其他语言。"
                },
                {
                    "role": "user",
                    "content": "你好，我叫李明，我从上海来。请你用中文给我打个招呼。"
                }
            ],
            "temperature": 0.3,
            "stream": false
        };

        document.getElementById('requestBody').value = JSON.stringify(defaultRequestBody, null, 2);

        async function sendRequest() {
            const apiKey = document.getElementById('apiKey').value;
            const endpointUrl = document.getElementById('endpointUrl').value;
            const requestBodyRaw = document.getElementById('requestBody').value;
            
            const responseContainer = document.getElementById('response-container');
            const statusDiv = document.getElementById('status');
            
            responseContainer.textContent = '正在发送请求...';
            statusDiv.innerHTML = '';

            if (!apiKey) {
                responseContainer.textContent = '错误：请输入 API 密钥。';
                statusDiv.innerHTML = `<div class="status error">错误：请输入 API 密钥。</div>`;
                return;
            }

            let requestBody;
            try {
                requestBody = JSON.parse(requestBodyRaw);
            } catch (e) {
                responseContainer.textContent = `请求体 JSON 格式错误: ${e.message}`;
                statusDiv.innerHTML = `<div class="status error">请求体 JSON 格式错误: ${e.message}</div>`;
                return;
            }

            try {
                const response = await fetch(endpointUrl, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${apiKey}`
                    },
                    body: JSON.stringify(requestBody)
                });

                const statusClass = response.ok ? 'success' : 'error';
                statusDiv.innerHTML = `<div class="status ${statusClass}">HTTP 状态码: ${response.status} ${response.statusText}</div>`;

                const responseData = await response.json();
                responseContainer.textContent = JSON.stringify(responseData, null, 2);

            } catch (error) {
                statusDiv.innerHTML = `<div class="status error">请求失败</div>`;
                responseContainer.textContent = `网络或请求错误: ${error.message}`;
            }
        }
    </script>
</body>
</html> 